package tester;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;

public class Animator extends Actor implements ApplicationListener, InputProcessor {

	public static void main(String[] args) {
		int WINDOW_WIDTH = 480;
		int WINDOW_HEIGHT = 480;

		new LwjglApplication(new Animator(), "Game", WINDOW_WIDTH,
				WINDOW_HEIGHT, false);
	}

	private static final int FRAME_COLS = 6; // #1
	private static final int FRAME_ROWS = 5; // #2

	Animation walkAnimation; // #3
	Texture walkSheet; // #4
	TextureRegion[] walkFrames; // #5
	SpriteBatch spriteBatch; // #6
	TextureRegion currentFrame; // #7
	int xCurrentPosition;
	int yCurrentPosition;
	int animationStartX;
	int animationStartY;
	int animationEndX;
	int animationEndY;
	long animationStartTime;
	long animationEndTime;

	float stateTime; // #8

	boolean isAnimationRunnig;

	@Override
	public void create() {
		walkSheet = new Texture(Gdx.files.internal("data/animation_sheet.png")); // #9
		TextureRegion[][] tmp = TextureRegion.split(walkSheet,
				walkSheet.getWidth() / FRAME_COLS, walkSheet.getHeight()
						/ FRAME_ROWS); // #10
		walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
		int index = 0;
		for (int i = 0; i < FRAME_ROWS; i++) {
			for (int j = 0; j < FRAME_COLS; j++) {
				walkFrames[index++] = tmp[i][j];
			}
		}
		walkAnimation = new Animation(0.025f, walkFrames); // #11
		spriteBatch = new SpriteBatch(); // #12
		stateTime = 0f; // #13

		isAnimationRunnig = true;
		animationStartTime = System.currentTimeMillis();
		animationEndTime = System.currentTimeMillis() + 3000;
		animationStartX = 50;
		animationStartY = 50;
		animationEndX = 300;
		animationEndY = 100;
	}

	@Override
	public void render() {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); // #14
		spriteBatch.begin();
		UpdateSprite();
		UpdateAnimationPosition();
		spriteBatch.draw(currentFrame, xCurrentPosition, yCurrentPosition); // #17
		spriteBatch.end();
	}

	private void UpdateSprite() {
		if (isAnimationRunnig) {
			stateTime += Gdx.graphics.getDeltaTime(); // #15
			currentFrame = walkAnimation.getKeyFrame(stateTime, true); // #16
		} else {
			currentFrame = walkAnimation.getKeyFrame(0, true);
		}

	}

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	private void UpdateAnimationPosition() {

		if (isAnimationRunnig) {
			long now = System.currentTimeMillis();
			if (now < animationEndTime) {
				long time = now - animationStartTime;
				long duration = animationEndTime - animationStartTime;
				float xspeed = (animationEndX - animationStartX)
						/ (float) duration;
				float yspeed = (animationEndY - animationStartY)
						/ (float) duration;

				xCurrentPosition = animationStartX
						+ (int) ((float) time * xspeed);
				yCurrentPosition = animationStartY
						+ (int) ((float) time * yspeed);
				/*System.out.println("xspeed=" + xspeed + " yspeed=" + yspeed
						+ " time=" + time + " duration=" + duration + " x="
						+ xCurrentPosition + " y=" + yCurrentPosition);*/

			} else {
				// STOP ANIMATION
				isAnimationRunnig = false;
				xCurrentPosition = animationStartX = animationEndX;
				yCurrentPosition = animationStartY = animationEndY;
			}
		} else {
			xCurrentPosition = animationStartX = animationEndX;
			yCurrentPosition = animationStartY = animationEndY;
		}

	}


	public void draw(SpriteBatch arg0, float arg1) {
		
	}

	
	public Actor hit(float arg0, float arg1) {
		return x > 0 && x < width && y > 0 && y < height ? this : null;
	}

	public boolean touchDown(float arg0, float arg1, int arg2) {
		System.out.println(arg0 + " " + arg1 + " " + arg2 );
		
		if (isAnimationRunnig) {
			isAnimationRunnig = false;
		} else {
			isAnimationRunnig = true;
			animationEndX = (int)arg0;
			animationEndY = (int)arg1;
		}

		return true;
	}

	public void touchDragged(float arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}


	public void touchUp(float arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		System.out.println("touchUp");		
	}


	public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		System.out.println("touchDown");
		return true;
	}

	@Override
	public boolean touchDragged(int arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean touchMoved(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean keyDown(int arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyTyped(char arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyUp(int arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean scrolled(int arg0) {
		// TODO Auto-generated method stub
		return false;
	}


}